package cn.edu.nju.askinginhand.util;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import cn.edu.nju.askinginhand.constant.TempFilePath;

public class MyAudioRecorder {
	private MediaRecorder mr;
	private File file;

	public void startRecord() {
		
		String path = TempFilePath.getAudioFilePath();
		file = new File(path);
		
		mr = new MediaRecorder();
		mr.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
		mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
		mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
		mr.setOutputFile(file.getAbsolutePath());

		try {
			file.createNewFile();
			mr.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mr.start();
	}

	public void stopRecord() {
		if (mr != null) {
			mr.stop();
			mr.release();
			mr = null;
		}

	}

	public File getFile() {
		return file;
	}
}
